#include "communication-device-doorlock.h"
#include "smart-device-database.h"

CommunicationDeviceDoorlock::CommunicationDeviceDoorlock(SmartDeviceManager *manager) : smartDeviceManger(manager)
{
    handle["add"] = &CommunicationDeviceDoorlock::add;
    handle["remove"] = &CommunicationDeviceDoorlock::remove;
    handle["open"] = &CommunicationDeviceDoorlock::open;
    handle["close"] = &CommunicationDeviceDoorlock::close;
    handle["query"] = &CommunicationDeviceDoorlock::query;
}

void CommunicationDeviceDoorlock::communication(SmartDeviceNetProtocol &netprotocol, NetCommunication *netobj)
{
    string command = netprotocol.getCommand();
    auto func = handle[command];
    if (!func)
    {
        return;
    }
    (this->*func)(netprotocol, netobj);
}

void CommunicationDeviceDoorlock::query(SmartDeviceNetProtocol &netprotocol,
                                        NetCommunication *netobj)
{
    string command = netprotocol.getCommand();
    uint32_t deviceType = netprotocol.getDeviceType();
    uint32_t deviceNumber = netprotocol.getDeviceNumber();
    uint8_t lockStatu;
    bool ok = smartDeviceManger->queryDoorlLock(deviceType, deviceNumber, lockStatu);
    string errmsg = ok ? "" : "error";
    string data = lockStatu ? "1" : "0";
    replay(netobj, command, errmsg, data);

    return;
}

void CommunicationDeviceDoorlock::add(SmartDeviceNetProtocol &netprotocol, NetCommunication *netobj)
{
    SmartDeviceDatabase *smartDeviceDatabase = SmartDeviceDatabase::getSmartDeviceDatabase();
    bool ok = smartDeviceDatabase->addDevice(
        netprotocol.getOperation(),
        netprotocol.getDeviceValue("deviceType"),
        netprotocol.getDeviceValue("deviceNumber"));

    string errmsg = ok ? "" : "error";
    // 等待锁那边回复 UDP发送
    replay(netobj, netprotocol.getCommand(), errmsg);
}

void CommunicationDeviceDoorlock::remove(SmartDeviceNetProtocol &netprotocol, NetCommunication *netobj)
{
    SmartDeviceDatabase *smartDeviceDatabase = SmartDeviceDatabase::getSmartDeviceDatabase();
    bool ok = smartDeviceDatabase->removeDevice(
        netprotocol.getOperation(),
        netprotocol.getDeviceValue("deviceType"),
        netprotocol.getDeviceValue("deviceNumber"));

    string errmsg = ok ? "" : "error";
    // 等待锁那边回复 UDP发送
    replay(netobj, netprotocol.getCommand(), errmsg);
}

void CommunicationDeviceDoorlock::open(SmartDeviceNetProtocol &netprotocol, NetCommunication *netobj)
{
    string command = netprotocol.getCommand();
    uint32_t deviceType = netprotocol.getDeviceType();
    uint32_t deviceNumber = netprotocol.getDeviceNumber();
    // 跟uart I2c spi 通讯去了
    bool ok = smartDeviceManger->openDoorlLock(deviceType, deviceNumber);
    // 2返回门锁控制结果 网络通讯协议返回给QT层
    string errmsg = ok ? "" : "error";
    replay(netobj, command, errmsg);
    return;
}

void CommunicationDeviceDoorlock::close(SmartDeviceNetProtocol &netprotocol, NetCommunication *netobj)
{
    string command = netprotocol.getCommand();
    uint32_t deviceType = netprotocol.getDeviceType();
    uint32_t deviceNumber = netprotocol.getDeviceNumber();
    // 跟uart I2c spi 通讯去了
    bool ok = smartDeviceManger->closeDoorlLock(deviceType, deviceNumber);
    // 2返回门锁控制结果 网络通讯协议返回给QT层
    string errmsg = ok ? "" : "error";
    replay(netobj, command, errmsg);
    return;
}

void CommunicationDeviceDoorlock::replay(NetCommunication *netobj,
                                         const string &command,
                                         const string &errmsg,
                                         const string &data)
{

    SmartDeviceNetProtocol netProtocol;
    netProtocol.addCommand("doorlock");
    netProtocol.addCommand(command);
    netProtocol.addResult(errmsg, data);

    string replyJsonData = netProtocol.toString();
    cout << "打印回复的json字符串" << replyJsonData << endl;
    // string 转  vector
    vector<uint8_t> packet;
    packet.assign(replyJsonData.begin(), replyJsonData.end());
    bool ret = netobj->sendPacket(packet);
    if (ret != true)
    {
        fprintf(stderr, "Fail to sendPacket \n");
        return;
    }

    return;
}
